import org.junit.Test;
import org.learn.spring5.entiy.User;
import org.learn.spring5.service.impl.UserService;
import org.learn.spring5.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class TestSpring5 {
    /**
     * 操作JdbcTemplate
     */
    @Test
    public void testAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        User user = new User();
        user.setId(1);
        user.setUserName("张三");
        user.setStatus(1);
        userService.addUser(user);

    }

    //JdbcTemplate修改操作
    @Test
    public void testUpdate() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        User user = new User();
        user.setId(1);
        user.setUserName("张四");
        user.setStatus(0);
        userService.update(user);
    }

    //JdbcTemplate删除操作
    @Test
    public void testDel() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        userService.del(1);
    }


    //JdbcTemplate 查询操作，返回用户的数量
    @Test
    public void testQueryCount() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        int count = userService.queryCount();
        System.out.println(count);
    }


    //JdbcTemplate 查询操作，返回对象根据id
    @Test
    public void testQueryById() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        User user = userService.queryById(1);
        System.out.println(user);
    }


    //JdbcTemplate 查询操作，返回集合对象
    @Test
    public void testFindAll() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        List<User> all = userService.findAll();
        System.out.println(all);
    }

    //JdbcTemplate 批量添加操作
    @Test
    public void batchAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        List<Object[]> list = new ArrayList<Object[]>();
        Object[] o1 = {"4", "name1", 0};
        Object[] o2 = {"5", "name2", 0};
        Object[] o3 = {"6", "name3", 0};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        userService.batchAdd(list);
    }

    //JdbcTemplate 批量修改操作
    @Test
    public void batchUpdate() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        List<Object[]> list = new ArrayList<Object[]>();
        Object[] o1 = {"namexiugai", 1, 4};
        Object[] o2 = {"name2xiugai", 1, 5};
        Object[] o3 = {"name3xiugai", 1, 6};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        userService.batchUpdate(list);
    }

    //JdbcTemplate 批量删除操作
    @Test
    public void batchDel() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        List<Object[]> list = new ArrayList<Object[]>();
        Object[] o1 = {4};
        Object[] o2 = {5};
        Object[] o3 = {6};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        userService.batchDel(list);
    }

}
